04. Defining Custom Annotations

Defining Custom Annotations

In this section, you will learn how to define your own custom Java annotations.

ND079 JPND C2 L04 A05 Defining Custom Annotations

Defining Custom Annotations

In addition to the built-in annotations from the previous section, you can also define your own custom Java annotations.

Example

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)  // Applies to class, interface, or enum
public @interface ConvertsTo {
  Class<?> targetClass();
  String setterPrefix() default "set";
}

Elements

Annotations are allowed to have parameterized values, which are called elements. Because annotations are always available at compile-time, the type of an element has to be a compile-time constant, an enum, a class literal, or an array initializer.

Elements can also have default values. Element values are set like so:

Object input = ... ;  // Let's pretend we know this is a List<String>
@SuppressWarnings(value = "unchecked")
List<String> result = (List<String>) input;

If an annotation only has one element, and the element is named "value", you can skip naming the "value":

Object input = ... ;  // Let's pretend we know this is a List<String>
@SuppressWarnings("unchecked")
List<String> result = (List<String>) input;

Retention Policies

Here are the possible retention policies for an annotation:

Retention Policy Description
SOURCE Annotation only exists in the source code.
RUNTIME Annotation exists in the .class bytecode file and is available at runtime to be used with reflection.
CLASS Annotation exists in the .class bytecode file but not exist while the program is running.

QUIZ QUESTION::

Match each RetentionPolicy with its description.

ANSWER CHOICES:



Description

Retention policy

Only exists in the source code

Exists in the .class bytecode file and is available at runtime to be used with reflection.

Exists in the .class bytecode file but not while the program is running.

SOLUTION:

Description

Retention policy

Only exists in the source code

Exists in the .class bytecode file but not while the program is running.

Exists in the .class bytecode file and is available at runtime to be used with reflection.

Annotation Targets

The target types determine which parts of the program can be given a particular annotation. Here are the possible target types:

Element Type Description
ANNOTATION_TYPE Annotation type declarations (for annotations that apply to other annotations).
CONSTRUCTOR Constructor declarations
FIELD Field declarations, including enum constants.
LOCAL_VARIABLE Local variable declarations.
METHOD Method declarations.
PACKAGE Package declarations.
PARAMETER Method parameter declarations.
TYPE Type declarations, such as classes, interfaces, annotation types, and enum declarations.

Which of the following are not required when defining a Java annotation.

SOLUTION:
  • A retention policy specified with `@Retention`
  • One or more target element types specified with `@Target`
  • Implementing the `Annotation` interface.

Further Reading